﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSCRMSendReportInEmail.wsSSRS;
using System.Web.Services.Protocols;

namespace MSCRMSendReportInEmail
{
    static class ReportMethods
    {
        public static string TurnToPdf(string i_ReportPath, string i_Format)
        {
            ReportExecutionService rs = new ReportExecutionService();
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

            // Render arguments
            string encoded = "";
            byte[] result = null;
            string reportPath = i_ReportPath;
            string format = i_Format;
            string historyID = null;
            string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

            //DataSourceCredentials[] credentials = null;
            //string showHideToggle = null;
            string encoding;
            string mimeType;
            string extension;
            Warning[] warnings = null;
            //ParameterValue[] reportHistoryParameters = null;
            string[] streamIDs = null;

            ExecutionInfo execInfo = new ExecutionInfo();
            ExecutionHeader execHeader = new ExecutionHeader();

            rs.ExecutionHeaderValue = execHeader;
            rs.Timeout = 9999999;
            execInfo = rs.LoadReport(reportPath, historyID);

            //rs.SetExecutionParameters(parameters, "en-us");
            String SessionId = rs.ExecutionHeaderValue.ExecutionID;

            Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);


            try
            {
                result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
                execInfo = rs.GetExecutionInfo();
                encoded = System.Convert.ToBase64String(result);

                Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.OuterXml);
                encoded = "";
            }


            return encoded;

        }
    }
}
